﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Arizu.Repository;
using System.Data;
using System.IO;

namespace AiRiZu
{
    public partial class EditRoom1 : System.Web.UI.Page
    {
        public int imgcount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        protected void Bind()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["roomId"]))
            {
                string roomID = Request.QueryString["roomId"].ToString();

                DataTable dt = Arizu.Repository.Main.CreateInstance().GetName(roomID);

                DataTable mydt = Arizu.Repository.Main.CreateInstance().GetRoom(roomID);

                DataTable imgdt = Main.CreateInstance().ImgRoomNum(roomID);

                if (dt.Rows.Count > 0)
                {
                    this.xing.Text = dt.Rows[0]["Xing"].ToString();
                    this.ming.Text = dt.Rows[0]["Ming"].ToString();
                }
                if (mydt.Rows.Count > 0)
                {
                    this.Phone.Text = mydt.Rows[0]["Phone"].ToString();
                    this.Email.Text = mydt.Rows[0]["Email"].ToString();
                    this.Province.Text = mydt.Rows[0]["Province"].ToString();
                    this.City.Text = mydt.Rows[0]["City"].ToString();
                    this.Area.Text = mydt.Rows[0]["Area"].ToString();
                    this.Adressdesc.Text = mydt.Rows[0]["Adressdesc"].ToString();
                    this.RoomType.SelectedValue = mydt.Rows[0]["RoomType"].ToString().Trim();
                    this.OftenServer.SelectedValue = mydt.Rows[0]["OftenServer"].ToString().Trim();
                    this.DuoTaoFangYuan.SelectedValue = mydt.Rows[0]["DuoTaoFangYuan"].ToString().Trim();
                    this.RoomNum.Text = mydt.Rows[0]["RoomNum"].ToString();
                    this.FaPiao.SelectedValue = mydt.Rows[0]["FaPiao"].ToString().Trim();
                    this.JiaGe.Text = mydt.Rows[0]["JiaGe"].ToString();
                    this.RoomTitle.Text = mydt.Rows[0]["RoomTitle"].ToString();
                    this.Issend.SelectedValue = mydt.Rows[0]["Issend"].ToString().Trim();
                    this.YouHuiJiaGe.Text = mydt.Rows[0]["YouHuiJiaGe"].ToString();
                    this.SendTime.Text = mydt.Rows[0]["SendTime"].ToString();
                    this.HousesType.Text = mydt.Rows[0]["HousesType"].ToString();
                    this.PersonNum.Text = mydt.Rows[0]["PersonNum"].ToString();
                    this.BedroomNum.Text = mydt.Rows[0]["BedroomNum"].ToString();
                    this.Bedtype.Text = mydt.Rows[0]["Bedtype"].ToString();
                    this.Bathroom.Text = mydt.Rows[0]["Bathroom"].ToString();
                    this.Leastdays.Text = mydt.Rows[0]["Leastdays"].ToString();
                    this.Mostdays.Text = mydt.Rows[0]["Mostdays"].ToString();
                    this.Checktime.Text = mydt.Rows[0]["Checktime"].ToString();
                    this.Checkouttime.Text = mydt.Rows[0]["Checkouttime"].ToString();
                    this.Mianji.Text = mydt.Rows[0]["Mianji"].ToString();
                    this.IsTeSe.SelectedValue = mydt.Rows[0]["IsTeSe"].ToString().Trim();
                    this.IsTeJia.SelectedValue = mydt.Rows[0]["IsTeJia"].ToString().Trim();
                    this.RecommendedReason.Text = mydt.Rows[0]["RecommendedReason"].ToString();
                }
                if (imgdt.Rows.Count > 0)
                {
                    for (int i = 0; i < 26; i++)
                    {
                        if (imgdt.Rows[0]["Image" + i].ToString() == "" || imgdt.Rows[0]["Image" + i] == null)
                        {
                            imgcount = i;
                            break;
                        }
                        if (i == 25)
                        {
                            imgcount = 26;
                        }
                    }
                }
            }




        }

        protected void Save_Click(object sender, EventArgs e)
        {
            Arizu.Entity.SendRoom sr = new Arizu.Entity.SendRoom();

            //更新数据
            if (!string.IsNullOrEmpty(Request.QueryString["roomId"]))
            {
                string roomID = Request.QueryString["roomId"].ToString();
                string sql = "update SendRoom set Name='" + this.xing.Text + this.ming.Text + "', Phone='" + this.Phone.Text + "',Email='" + this.Email.Text;
                sql += "',Province='" + this.Province.Text + "',City='" + this.City.Text + "',Area='" + this.Area.Text + "',Adressdesc='" + this.Adressdesc.Text;
                sql += "',RoomType='" + this.RoomType.SelectedValue + "',OftenServer='" + this.OftenServer.SelectedValue + "',DuoTaoFangYuan='" + this.DuoTaoFangYuan.SelectedValue;
                sql += "',RoomNum='" + this.RoomNum.Text + "',FaPiao='" + this.FaPiao.SelectedValue + "',JiaGe='" + this.JiaGe.Text + "',RoomTitle='" + this.RoomTitle.Text;
                sql += "',Issend='" + this.Issend.SelectedValue + "',YouHuiJiaGe='" + this.YouHuiJiaGe.Text + "',HousesType='" + this.HousesType.Text + "',PersonNum='" + this.PersonNum.Text + "',BedroomNum='" + this.BedroomNum.Text + "',Bedtype='" + this.Bedtype.Text + "',Bathroom='" + this.Bathroom.Text + "',Leastdays='" + this.Leastdays.Text + "',Mostdays='" + this.Mostdays.Text + "',Checktime='" + this.Checktime.Text + "',Checkouttime='" + this.Checkouttime.Text + "',Mianji='" + this.Mianji.Text + "',IsTeSe='" + this.IsTeSe.SelectedValue + "',IsTeJia='" + this.IsTeJia.SelectedValue + "',RecommendedReason='" + this.RecommendedReason.Text + "' where SendRoomid='" + roomID + "'";
                int i = Main.CreateInstance().SaveRoom(sql);
                if (i > 0)
                {
                    Arizu.Repository.Message.Alertback("保存成功！");
                }
                else
                {
                    Arizu.Repository.Message.Alertback("保存失败！");
                }
            }
            //创建房间
            else
            {
                sr.Name = this.xing.Text + this.ming.Text;
                sr.Phone = this.Phone.Text;
                sr.Email = this.Email.Text;
                sr.Province = this.Province.Text;
                sr.City = this.City.Text;
                sr.Area = this.Area.Text;
                sr.Adressdesc = this.Adressdesc.Text;
                sr.RoomType = this.RoomType.SelectedValue;
                sr.OftenServer = this.OftenServer.SelectedValue;
                sr.DuoTaoFangYuan = this.DuoTaoFangYuan.SelectedValue;
                sr.RoomType = this.RoomNum.Text;
                sr.FaPiao = this.FaPiao.SelectedValue;
                sr.JiaGe = this.JiaGe.Text;
                sr.RoomTitle = this.RoomTitle.Text;
                sr.Issend = this.Issend.SelectedValue;
                sr.YouHuiJiaGe = this.YouHuiJiaGe.Text;
                sr.HousesType = this.HousesType.Text;
                sr.PersonNum = int.Parse(this.PersonNum.Text);
                sr.BedroomNum = int.Parse(this.BedroomNum.Text);
                sr.Bedtype = this.Bedtype.Text;
                sr.Bathroom = int.Parse(this.Bathroom.Text);
                sr.Leastdays = int.Parse(this.Leastdays.Text);
                sr.Mostdays = this.Mostdays.Text;
                sr.Checktime = this.Checktime.Text;
                sr.Checkouttime = this.Checkouttime.Text;
                sr.Mianji = this.Mianji.Text;
                sr.IsTeSe = this.IsTeSe.SelectedValue;
                sr.IsTeJia = this.IsTeJia.SelectedValue;
                sr.RecommendedReason = this.RecommendedReason.Text;
                int i = Main.CreateInstance().InsertInfo(sr);
                if (i > 0)
                {
                    Arizu.Repository.Message.Alertback("保存成功！");
                }
                else
                {
                    Arizu.Repository.Message.Alertback("保存失败！");
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = this.FileUpload1.FileName;
            FileInfo file = new FileInfo(name);
            string tmpName = System.IO.Path.GetTempFileName();
            tmpName = tmpName.Substring(tmpName.LastIndexOf("\\") + 1, tmpName.Length - (tmpName.LastIndexOf("\\") + 1)).Replace(".tmp", "");
            string mm = DateTime.Now.Millisecond.ToString();
            name = tmpName.Substring(0, 5) + mm + file.Extension;

            if (!string.IsNullOrEmpty(Request.QueryString["roomId"]))
            {
                DataTable imgdt = Main.CreateInstance().ImgRoomNum(Request.QueryString["roomId"].ToString());
                //更新房间图片
                if (imgdt.Rows.Count > 0)
                {
                    for (int i = 0; i < 26; i++)
                    {
                        if (string.IsNullOrEmpty(imgdt.Rows[0]["Image" + i].ToString()))
                        {
                            string sql = "update ImageSendRoom set Image" + i + "='" + name + "' where SendRoomid=" + Request.QueryString["roomId"].ToString();
                            int j = Main.CreateInstance().InsertRoom(sql);
                            if (j > 0)
                            {
                                FileUpload1.SaveAs(Server.MapPath("imgs/" + name));
                                Arizu.Repository.Message.Alert("图片上传成功！");
                                break;
                            }
                            else
                            {
                                Arizu.Repository.Message.Alert("图片上传失败！");
                            }
                        }
                    }
                    for (int i = 0; i < 26; i++)
                    {
                        if (imgdt.Rows[0]["Image" + i].ToString() == "" || imgdt.Rows[0]["Image" + i] == null)
                        {
                            imgcount = i;
                            break;
                        }
                        if (i == 25)
                        {
                            imgcount = 26;
                        }
                    }
                }
                //插入房间图片数据
                else
                {
                    string sql = "insert into ImageSendRoom(SendRoomid,Image0)values(" + Request.QueryString["roomId"].ToString() + ",'" + name + "')";
                    int j = Main.CreateInstance().InsertRoom(sql);
                    if (j > 0)
                    {
                        FileUpload1.SaveAs(Server.MapPath("imgs/" + name));
                        Arizu.Repository.Message.Alert("图片上传成功！");
                    }
                    else
                    {
                        Arizu.Repository.Message.Alert("图片上传失败！");
                    }
                    DataTable mydt = Main.CreateInstance().ImgRoomNum(Request.QueryString["roomId"].ToString());
                    for (int i = 0; i < 26; i++)
                    {
                        if (mydt.Rows[0]["Image" + i].ToString() == "" || mydt.Rows[0]["Image" + i] == null)
                        {
                            imgcount = i;
                            break;
                        }
                        if (i == 25)
                        {
                            imgcount = 26;
                        }
                    }
                }
            }
        }
    }
}
